package com.jinrustar.op.core.base;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseModel> extends ServiceImpl<M, T> {

    public List<Long> selectIdByMap(Pagination pagination, Map params) {
        return this.baseMapper.selectIdsByMap(pagination, params);
    }

    public T add(T t) {
        this.insert(t);
        return t;
    }


    /**
     * 根据 map(通常是请求参数) 构建 Page　对象
     *
     * @param params
     * @return
     */
    public Page<T> generatePage(Map params) {
        Object pageNum = params.get("pageNum");
        Object pageSize = params.get("pageSize");
        Object orderBy = params.get("orderBy");
        int pn = 1;
        int size = 10;
        String orderByStr = "";
        if (StringUtils.isNotEmpty((CharSequence) pageNum)) {
            pn = Integer.parseInt((String) pageNum);
        }
        if (StringUtils.isNotEmpty((CharSequence) pageSize)) {
            size = Integer.parseInt((String) pageSize);
        }
        if (StringUtils.isNotEmpty((CharSequence) orderBy)) {
            orderByStr = (String) orderBy;
        }
        return new Page<T>(pn, size, orderByStr);
    }
}
